package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.ProductManageModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class GoodsManageAdapter extends BaseAdapter {


    private Context context;
    private LayoutInflater inflater;
    List<ProductManageModel> goodsList;
    private BaseActivity baseActivity;
    private String TAG = "GoodsManageAdapter";
    private Holder holder;

    public interface IManageProductListener {
        void delete(ProductManageModel model);

        void edit(ProductManageModel model);

        void changeStatus(ProductManageModel model);
    }

    private IManageProductListener mListener;

    public void setManageListener(IManageProductListener listener) {
        mListener = listener;
    }


    public GoodsManageAdapter(Context context) {
        this.context = context;
        goodsList = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<ProductManageModel> datas) {
        goodsList.clear();
        goodsList.addAll(datas);
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_list_goods_manage, null);
            holder = new Holder();
            holder.text_goods_name = (TextView) view.findViewById(R.id.text_goods_name);
            holder.text_goods_money = (TextView) view.findViewById(R.id.text_goods_money);
            holder.text_sold_num = (TextView) view.findViewById(R.id.text_sold_num);
            holder.text_stock_num = (TextView) view.findViewById(R.id.text_stock_num);
            holder.text_upordown_goods = (TextView) view.findViewById(R.id.text_upordown_goods);
            holder.text_delete_goods = (TextView) view.findViewById(R.id.text_delete_goods);
            //holder.text_edit_goods= (TextView) view.findViewById(R.id.text_edit_goods);
            holder.img_goods_picture = (ImageView) view.findViewById(R.id.img_goods_picture);


            view.setTag(holder);

        } else {
            holder = (Holder) view.getTag();
        }

        holder.text_delete_goods.setTag(i);
        holder.text_upordown_goods.setTag(i);

        ImageLoader.getInstance().displayImage(goodsList.get(i).getTpPic(), holder.img_goods_picture);

        holder.text_goods_name.setText(goodsList.get(i).getTpName());
        holder.text_goods_money.setText(goodsList.get(i).getTpSalePrice());
        holder.text_sold_num.setText(goodsList.get(i).getTpSalesCount());
        holder.text_stock_num.setText(goodsList.get(i).getTpStore());

        if("1".equals(goodsList.get(i).getStatus())){
            holder.text_upordown_goods.setText("下架商品");
        }else{
            holder.text_upordown_goods.setText("上架商品");
        }
        holder.text_upordown_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.changeStatus(goodsList.get(i));
            }
        });
        holder.text_delete_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.delete(goodsList.get(i));
            }
        });

        return view;
    }


    public static class Holder {
        public TextView text_goods_name, text_goods_money, text_sold_num,
                text_stock_num, text_upordown_goods, text_delete_goods,text_edit_goods;
        public ImageView img_goods_picture;
    }


}
